#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_XHAMSTER_REGEXP_URL="http://\(*\)\?xhamster\.com/"

MODULE_XHAMSTER_DOWNLOAD_OPTIONS=""
MODULE_XHAMSTER_DOWNLOAD_RESUME=no
MODULE_XHAMSTER_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no
MODULE_XHAMSTER_LIST_OPTIONS=""

xhamster_download() {

local COOKIEFILE=$1
local URL=$2
temp=""
temb=""
almost=""
tryingsomethingcompletelydifferent=""
tryingsomethingcompletelynew1=""
tryingsomethingcompletelyoriginal2=""
temp3=$tryingsomethingcompletelydifferent2$tryingsomethingcompletelydifferent


	Pageinit=$(curl -k "$URL") || return


	tryingsomethingcompletelynew1=$(echo "$Pageinit" | grep -m1 'srv') 
	tryingsomethingcompletelyoriginal2=$(echo "$tryingsomethingcompletelynew1" | replace "'srv': '" "" ) || return
	tryingsomethingcompletelyoriginal2=$(echo "$tryingsomethingcompletelyoriginal2" | replace "'," "/key=" ) || return

#	tryingsomethingcompletelydifferent=$(echo "$Pageinit" | grep -m1 'file') 
#	tryingsomethingcompletelydifferent=$(echo "$tryingsomethingcompletelydifferent" | replace "'file': '" "" ) || return
#	tryingsomethingcompletelydifferent=$(echo "$tryingsomethingcompletelydifferent" | replace "'," "" ) || return
#	tryingsomethingcompletelydifferent=$(echo "$tryingsomethingcompletelydifferent" | trim ) || return


#	temp=$(echo $tryingsomethingcompletelydifferent | strip) || return
	temp=$(echo $tryingsomethingcompletelyoriginal2 | strip) || return
#	echo $temp > temp
temp=${temp%%&image*}
temp=${temp##*srv=}
temp=${temp/&file=/\/key=}
temp=$(echo $temp | uri_decode)
	
	alamo=$(curl -k -I "$temp" | grep 'Location:') || return
	alamo=$(echo "$alamo" | replace "Location: " "") || return
#	almost=$(echo $almost | replace "Location: " "") || return
	FILE_URL=$(echo "$alamo" | strip) || return
  FILE_NAME=$(echo "$Pageinit" | parse_tag title)
  FILE_NAME=${FILE_NAME/ - xHamster.com/.flv}
  
	# echo $tryingsomethingcompletelydifferent
	echo $FILE_URL
	echo $FILE_NAME
	# echo $temp
}